﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using YiSha.Entity.RedBookSetManage;
using YiSha.Model.Param.RedBookSetManage;
using YiSha.Util.Extension;
using YiSha.Data.Repository;
using System.Linq;
using YiSha.Util.Model;
using System.Data.Common;
using YiSha.Data;
using YiSha.Util;
using YiSha.Model.Param.RedBookFormManage;
using YiSha.Entity.RedBookFormManage;

namespace YiSha.Service.RedBookFormManage
{
    public class RedBookFormService : RepositoryFactory
    {
        #region 获取数据
        public async Task<List<RedBookFormEntity>> GetList(RedBookFormParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.OrderBy(p => p.Id).ToList();
        }

        public async Task<List<RedBookFormEntity>> GetPageList(RedBookFormParam param, Pagination pagination)
        {
            var strSql = new StringBuilder();
            List<DbParameter> filter = ListPageFilter(param, strSql);
            pagination.SortType = "desc";
            var list = await this.BaseRepository().FindList<RedBookFormEntity>(strSql.ToString(), filter.ToArray(), pagination);
            return list.ToList();
        }

        public async Task<RedBookFormEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<RedBookFormEntity>(id);
        }

        public async Task<int> GetMaxSort()
        {
            object result = await this.BaseRepository().FindObject("SELECT MAX(id) FROM SysRedBookForm");
            int sort = result.ParseToInt();
            sort++;
            return sort;
        }
        public async Task<List<SelectData>> GetUserDataList()
        {
            var list = await this.BaseRepository().FindList<SelectData>("SELECT  [Id] as [Key] ,[RealName] as [Value]  FROM [YiShaAdmin].[dbo].[SysUser]");
            return list.ToList();
        }
        #endregion

        #region 提交数据

        public async Task UpdateRedBookForm(RedBookFormEntity entity)
        {
            await this.BaseRepository().Update(entity);
        }

        public async Task SaveForm(RedBookFormEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                await entity.Create();
                await this.BaseRepository().Insert<RedBookFormEntity>(entity);
            }
            else
            {
                await entity.Modify();
                await this.BaseRepository().Update<RedBookFormEntity>(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
     
           
                long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
               await this.BaseRepository().Delete<RedBookFormEntity>(idArr);
          
         
        }
        #endregion




        #region 私有方法
        private Expression<Func<RedBookFormEntity, bool>> ListFilter(RedBookFormParam param)
        {
            var expression = LinqExtensions.True<RedBookFormEntity>();
            if (param != null)
            {
                if (!param.Id.IsEmpty())
                {
                    expression = expression.And(t => t.Id.Equals(param.Id));
                }
            }
            return expression;
        }
        private List<DbParameter> ListPageFilter(RedBookFormParam param, StringBuilder strSql)
        {
            strSql.Append(@"SELECT  a.*,b.RealName FROM SysRedBookForm a LEFT JOIN SysUser b on a.UserId=b.Id WHERE 1=1 ");

            var parameter = new List<DbParameter>();
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.Name))
                {
                    strSql.Append(" AND a.Name  like @Name");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@Name", '%' + param.Name + '%'));
                }
                if (param.Id > 0)
                {
                    strSql.Append(" AND a.Id = @Id");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@Id", param.Id));
                }
                if (!string.IsNullOrEmpty(param.Tel))
                {
                    strSql.Append(" AND a.Tel like @Tel");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@Tel", '%' + param.Tel + '%'));
                }
                if (!string.IsNullOrEmpty(param.City))
                {
                    strSql.Append(" AND a.City like @City");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@City", '%' + param.City + '%'));
                }
                if (param.CurrentUserId > 0)
                {
                    strSql.Append(" AND a.Userid = @CurrentUserId");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@CurrentUserId", param.CurrentUserId));
                }

            
            }
            return parameter;
        }
        #endregion
    }
}
